package com.hu.bgp.server;

import java.io.File;
import java.util.Hashtable;

import javax.servlet.ServletContext;

import com.hu.bgp.framework.ServerFacade;

import com.hu.bgp.framework.clientapi.ClientToServerMessage;


/**
 * Deze singleton beheert alle gametypes, games, enzovoort.
 * 
 * @author ejvos
 */
public class GameServerContext
{
	private static final String CONTEXT_KEY = "GameServer";

	public static GameServerContext getInstance(ServletContext servletContext)
	{
		Object object = servletContext.getAttribute(CONTEXT_KEY);
		GameServerContext instance = null;

		if (object == null || !(object instanceof GameServerContext))
		{
			instance = new GameServerContext(servletContext);
			servletContext.setAttribute(CONTEXT_KEY, instance);
		}
		else
		{
			instance = (GameServerContext) object;
		}
		return instance;
	}

	private GameTypeCollection gameTypeCollection = new GameTypeCollection();
	private Hashtable<String,ServerFacade > userGameTable = new Hashtable<String,ServerFacade>();
	private BgpConfiguration configuration;
	private BarFileSystemWatcher bfsWatcher;
	private String playerClientModulePath;

	private GameServerContext(ServletContext servletContext)
	{
		configuration = new BgpConfiguration(servletContext);
		bfsWatcher = new BarFileSystemWatcher(configuration, gameTypeCollection);
		// TODO extensions use filefilter... by ej
		bfsWatcher.addFileExtension("jar");
		bfsWatcher.start();
		playerClientModulePath = servletContext.getRealPath("/bgp");
	}

	public GameTypeCollection getGameTypeCollection()
	{
		return gameTypeCollection;
	}

	public GameType getGameTypeByAlias(String alias)
	{
		return gameTypeCollection.getByAlias(alias);
	}

	public BgpConfiguration getConfiguration()
	{
		return configuration;
	}
	public ServerFacade getUserGameInstance(String userID)
	{
		return userGameTable.get(userID);
	}
	public ServerFacade setUserGameInstance(String userID, ServerFacade gameInstance)
	{
		return userGameTable.put(userID, gameInstance);
	}
	public void initGameInstance(String creatorID, GameType game)
	{
		userGameTable.put(creatorID, game.initGameInstance());
	}
	public void initGameInstance(String creatorID, String alias)
	{
		GameType game = getGameTypeByAlias(alias);
		userGameTable.put(creatorID, game.initGameInstance());
	}
	
	public void dispatchMessageToGameInstance(ClientToServerMessage message)
	{
		userGameTable.get(message.getSenderID()).handleMessage(message);
	}

	public File getResourcesFileByContextPath(String contextPath)
	{
		String resourcesUrlPrefix = configuration.getResourcesUrlPrefix();
		String playerClientUrlPrefix = configuration.getPlayerClientUrlPrefix();
		File file = null;
		if (contextPath.startsWith(resourcesUrlPrefix))
		{
			String path = contextPath.substring(resourcesUrlPrefix.length());

			int firstSlash = path.indexOf('/');
			String alias = path.substring(0, firstSlash);
			String resourcePath = path.substring(firstSlash + 1);

			GameType gt = getGameTypeByAlias(alias);
			file = new File(gt.getFullPath() + File.separator + configuration.getBarResourceDirName() + File.separator + resourcePath);
		}
		else if (contextPath.startsWith(playerClientUrlPrefix))
		{
			String path = contextPath.substring(playerClientUrlPrefix.length());
			file = new File(playerClientModulePath + File.separator + path);
		}
		return file;
	}


}
